package Game;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.jogamp.opengl.util.texture.Texture;

import Physics.Vector3;

public class MenuButton {
	private Vector3	m_Center;
	private float m_ScreenX;
	private float m_ScreenY;
	private float m_Xf;
	private float m_Yf;
	private int m_id;
	private boolean m_IsPressed;
	private String m_Text;
	private BufferedImage m_TextureLit;
	private BufferedImage m_TextureUnlit;
	private FuriousTourist m_ParentGame;
	// TODO: NYI private boolean m_PlaySound;
	
	public MenuButton(int id, float xFrac, float yFrac, float bbWidth, float bbHeight, int h, int w, String lit, String unlit, String text, FuriousTourist parent)
	{
		// TODO: NYI m_PlaySound = true;
		m_id = id;
		
		m_Xf = xFrac;
		m_Yf = yFrac;
		
		m_ScreenX = xFrac * bbWidth;
		m_ScreenY = yFrac * bbHeight;
		
		m_Center = new Vector3((float)w/2, (float)h/2, 0.0f);
		m_Text = text;
		
		m_IsPressed = false;
		
		try {
			m_TextureLit = ImageIO.read(getClass().getResource(lit));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			m_TextureUnlit = ImageIO.read(getClass().getResource(unlit));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		m_ParentGame = parent;
	}
	
	public boolean isMouseOver()
	{
		// TODO: Get screen coordinate of mouse somehow
		Vector3 mousePos = new Vector3(0, 0, 0);
		
		float x1 = m_ScreenX - m_Center.x;
		float x2 = m_ScreenX + m_Center.x;
		float y1 = m_ScreenY - m_Center.y;
		float y2 = m_ScreenY + m_Center.y;
		
		if (mousePos.x > x1 &&
				mousePos.x < x2 &&
				mousePos.y > y1 &&
				mousePos.y < y2)
		{
			// if the cursor is over it, play a sound effect
			// TODO: NYI
			/*if (m_PlaySound)
			{
				SOUNDMANAGER->SoundPlay(SoundID.SOUND_MENUCLICK2);
				mPlaySound = false;
			}*/
			
			return true;
		}
		else
		{

			// if they take the cursor away, allow the sound to be triggered again
			// TODO: NYI
			//m_PlaySound = true;
			return false;
		}
	}
	
	public void updateSize(float bbWidth, float bbHeight)
	{
		m_ScreenX = m_Xf * bbWidth;
		m_ScreenY = m_Yf * bbHeight;
	}
	
	public void Pressed()
	{
		m_IsPressed = true;
	}
	
	public void Unpressed()
	{
		m_IsPressed = false;
	}
	
	public int GetID()
	{
		return m_id;
	}
	
	public void Render()
	{
		int l = (int)m_ScreenX - (int)m_Center.x;
		int r = (int)m_ScreenX + (int)m_Center.x;
		int t = (int)m_ScreenY - (int)m_Center.y;
		int b = (int)m_ScreenY + (int)m_Center.y;
		
		if (m_IsPressed)
			m_ParentGame.Draw2DImage(m_TextureLit, l, r, t, b);
		else
			m_ParentGame.Draw2DImage(m_TextureUnlit, l, r, t, b);
	}
}
